package org.he.org.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.he.basic.service.impl.BaseServiceImpl;
import org.he.org.domain.Department;
import org.he.org.domain.Employee;
import org.he.org.mapper.IEmployeeMapper;
import org.he.org.query.DepartmentQuery;
import org.he.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author He
 * @date 2023/11/30  10:41
 */
@Service
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private IEmployeeMapper employeeMapper;
//    @Override
//    public List<Department> getTree() {
//        return employeeMapper.getTree();
//    }

    @Override
    public List<String> queryPermissionsById(Long id) {
        return employeeMapper.queryPermissionsById(id);
    }


    @Override
    public void export(DepartmentQuery query) {
        try {
            //查询要导出的数据
            List<Employee> employees = employeeMapper.getExpExportList(query);
//            ExcelUtil.exportExcelX(employees, "员工导出表", "员工信息", Employee.class, "员工导出表.xlsx", response);
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("员工导出表", "员工信息", ExcelType.XSSF),
                    Employee.class,
                    employees);
            FileOutputStream outputStream = new FileOutputStream("F:\\imp\\Download.xlsx");
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Employee> upload(MultipartFile multipartFile) throws Exception {

        List<Employee> result = new ArrayList<>();
        List<Employee> exportErr = new ArrayList<>();
        //            System.out.println(multipartFile);
//            log.info("文件名:[{}]",multipartFile.getOriginalFilename());
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
//            FileInputStream fileInputStream = new FileInputStream((File) multipartFile);
        List<Employee> employees = ExcelImportUtil.importExcel(multipartFile.getInputStream(), Employee.class, params);
        employees.forEach(System.out::println);
        for (Employee employee : employees) {
            if (employee != null && employee.getEmail() != null && employee.getEmail().contains("@")) {
                String name = employee.getDepartment().getName();
                Long id = employeeMapper.queryDepName(name);
                employee.getDepartment().setId(id);
                employeeMapper.insert(employee);
            }
            if (employee != null && employee.getEmail() != null && !employee.getEmail().contains("@")) {
                result.add(employee);
                exportErr.add(employee);

            }
        }
        if (exportErr.size() > 0) {
            err(exportErr);
        }
//            employees.forEach(System.out::println);
        return result;
    }

    public void err(List<Employee> employees) {
        try {
            ExportParams params = new ExportParams("错误用户信息", "错误信息表", ExcelType.XSSF);
            Workbook workbook = ExcelExportUtil.exportExcel(params, Employee.class, employees);

            FileOutputStream fos = new FileOutputStream("F:\\imp\\error.xlsx");
            workbook.write(fos);
            fos.close();
            workbook.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
